package com.floreantpos.ui.views;

import java.awt.Dimension;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import com.floreantpos.main.Application;
import com.floreantpos.model.CreditCardTransaction;
import com.floreantpos.model.DebitCardTransaction;
import com.floreantpos.swing.DoubleDocument;
import com.floreantpos.ui.dialog.POSMessageDialog;
import com.floreantpos.ui.views.order.RootView;
import java.awt.Color;
import java.awt.Toolkit;

/**
 *
 * @author MShahriar
 */
public class CardPaymentView extends PaymentView {

  public final static int CARD_TYPE_CREDIT = 0;
  public final static int CARD_TYPE_DEBIT = 1;
  public final static int CARD_TYPE_BENEFIT = 2;

  public final static String MASTER_CARD = "MasterCard";
  public final static String VISA_CARD = "Visa";
  public final static String OTHER_CARD = "Outro";
  public final static String VR_CARD = "VR";
  public final static String TR_CARD = "TR";

  private int cardType = CARD_TYPE_CREDIT;
  private String whichCard = OTHER_CARD;

  private int guestID;
  private Boolean giftCertStatus;

  private double gratuityAmount;
  
  private String taxId;

  public CardPaymentView(String inputTaxId) {
    initComponents();

    DoubleDocument gratuityDocument = new DoubleDocument();
    gratuityDocument.addDocumentListener(new DocumentListener() {
      void updatePayAmount() {
        double cardAmount = 0;
        double gratuity = 0;

        try {
          gratuity = numberFormat.parse(tfGratuityAmount.getText()).doubleValue();
        } catch (Exception x) {

        }
        cardAmount = getDueAmount();
        tfCardAmount.setText(Application.formatNumber(cardAmount + gratuity));
      }

      public void insertUpdate(DocumentEvent e) {
        updatePayAmount();
      }

      public void removeUpdate(DocumentEvent e) {
        updatePayAmount();
      }

      public void changedUpdate(DocumentEvent e) {
        updatePayAmount();
      }

    });
    tfGratuityAmount.setDocument(gratuityDocument);
    tfCardAmount.setDocument(new DoubleDocument());
    guestID = 0;
    giftCertStatus = true;
    tfCardAmount.setFocusable(giftCertStatus);
    tfGratuityAmount.setFocusable(giftCertStatus);
    taxId = inputTaxId;
  }

  public CardPaymentView(Boolean giftCertEnabled, int registeredGuest, String inputTaxId) {
    initComponents();

    DoubleDocument gratuityDocument = new DoubleDocument();
    gratuityDocument.addDocumentListener(new DocumentListener() {
      void updatePayAmount() {
        double cardAmount = 0;
        double gratuity = 0;

        try {
          gratuity = numberFormat.parse(tfGratuityAmount.getText()).doubleValue();
        } catch (Exception x) {

        }
        cardAmount = getDueAmount();
        tfCardAmount.setText(Application.formatNumber(cardAmount + gratuity));
      }

      public void insertUpdate(DocumentEvent e) {
        updatePayAmount();
      }

      public void removeUpdate(DocumentEvent e) {
        updatePayAmount();
      }

      public void changedUpdate(DocumentEvent e) {
        updatePayAmount();
      }

    });
    tfGratuityAmount.setDocument(gratuityDocument);
    tfCardAmount.setDocument(new DoubleDocument());
    guestID = registeredGuest;
    giftCertStatus = giftCertEnabled;
    tfCardAmount.setFocusable(giftCertStatus);
    tfGratuityAmount.setFocusable(giftCertStatus);
    taxId = inputTaxId;
  }

  Dimension preferredSize = new Dimension(380, 100);

  public Dimension getPreferredSize() {
    return preferredSize;
  }

  private void initComponents() {
    java.awt.GridBagConstraints gridBagConstraints;

    buttonGroup1 = new javax.swing.ButtonGroup();
    transparentPanel3 = new com.floreantpos.swing.TransparentPanel();
    transparentPanel5 = new com.floreantpos.swing.TransparentPanel();
    btnChangePMethod = new com.floreantpos.swing.PosButton();
    btnCancel = new com.floreantpos.swing.PosButton();
    btnSettle = new com.floreantpos.swing.PosButton();
    transparentPanel8 = new com.floreantpos.swing.TransparentPanel();
    transparentPanel9 = new com.floreantpos.swing.TransparentPanel();
    posButton1 = new com.floreantpos.swing.PosButton();
    posButton2 = new com.floreantpos.swing.PosButton();
    posButton3 = new com.floreantpos.swing.PosButton();
    posButton4 = new com.floreantpos.swing.PosButton();
    posButton5 = new com.floreantpos.swing.PosButton();
    posButton6 = new com.floreantpos.swing.PosButton();
    posButton9 = new com.floreantpos.swing.PosButton();
    posButton8 = new com.floreantpos.swing.PosButton();
    posButton7 = new com.floreantpos.swing.PosButton();
    posButton10 = new com.floreantpos.swing.PosButton();
    posButton11 = new com.floreantpos.swing.PosButton();
    posButton12 = new com.floreantpos.swing.PosButton();
    transparentPanel10 = new com.floreantpos.swing.TransparentPanel();
    btnMasterCard = new com.floreantpos.swing.POSToggleButton();
    btnVisa = new com.floreantpos.swing.POSToggleButton();
    btnOther = new com.floreantpos.swing.POSToggleButton();
    btnVR = new com.floreantpos.swing.POSToggleButton();
    btnTR = new com.floreantpos.swing.POSToggleButton();
    transparentPanel7 = new com.floreantpos.swing.TransparentPanel();
    transparentPanel1 = new com.floreantpos.swing.TransparentPanel();
    jLabel1 = new javax.swing.JLabel();
    tfGratuityAmount = new javax.swing.JTextField();
    lblCSymbol1 = new javax.swing.JLabel();
    transparentPanel4 = new com.floreantpos.swing.TransparentPanel();
    jLabel3 = new javax.swing.JLabel();
    tfAuthorizationCode = new javax.swing.JTextField();
    jLabel4 = new javax.swing.JLabel();
    transparentPanel2 = new com.floreantpos.swing.TransparentPanel();
    jLabel2 = new javax.swing.JLabel();
    tfCardAmount = new javax.swing.JTextField();
    lblCSymbol2 = new javax.swing.JLabel();

    setLayout(new java.awt.BorderLayout(0, 5));

    setBorder(javax.swing.BorderFactory.createTitledBorder(null, com.floreantpos.POSConstants.TITLE, javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));
    transparentPanel3.setLayout(new java.awt.BorderLayout());

    add(transparentPanel3, java.awt.BorderLayout.WEST);

    transparentPanel5.setLayout(new java.awt.GridLayout(1, 0, 5, 5));

    transparentPanel5.setPreferredSize(new java.awt.Dimension(100, 60));
    btnChangePMethod.setText("<html><body><p align='center'>Alterar Forma Pagamento</p></body></html>");
    btnChangePMethod.setFocusable(false);
    btnChangePMethod.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnChangePMethodActionPerformed(evt);
      }
    });

    transparentPanel5.add(btnChangePMethod);

    btnCancel.setText(com.floreantpos.POSConstants.CANCEL);
    btnCancel.setFocusable(false);
    btnCancel.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnCancelActionPerformed(evt);
      }
    });

    transparentPanel5.add(btnCancel);

    btnSettle.setText(com.floreantpos.POSConstants.SETTLE);
    btnSettle.setFocusable(false);
    btnSettle.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnSettleActionPerformed(evt);
      }
    });

    transparentPanel5.add(btnSettle);

    add(transparentPanel5, java.awt.BorderLayout.SOUTH);

    transparentPanel8.setLayout(new java.awt.GridBagLayout());

    transparentPanel9.setLayout(new java.awt.GridLayout(0, 3, 5, 5));

    posButton1.setAction(calAction);
    posButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/7_32.png")));
    posButton1.setActionCommand("7");
    posButton1.setFocusable(false);
    transparentPanel9.add(posButton1);

    posButton2.setAction(calAction);
    posButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/8_32.png")));
    posButton2.setActionCommand("8");
    posButton2.setFocusable(false);
    transparentPanel9.add(posButton2);

    posButton3.setAction(calAction);
    posButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/9_32.png")));
    posButton3.setActionCommand("9");
    posButton3.setFocusable(false);
    transparentPanel9.add(posButton3);

    posButton4.setAction(calAction);
    posButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/4_32.png")));
    posButton4.setActionCommand("4");
    posButton4.setFocusable(false);
    transparentPanel9.add(posButton4);

    posButton5.setAction(calAction);
    posButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/5_32.png")));
    posButton5.setActionCommand("5");
    posButton5.setFocusable(false);
    transparentPanel9.add(posButton5);

    posButton6.setAction(calAction);
    posButton6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/6_32.png")));
    posButton6.setActionCommand("6");
    posButton6.setFocusable(false);
    transparentPanel9.add(posButton6);

    posButton9.setAction(calAction);
    posButton9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/1_32.png")));
    posButton9.setActionCommand("1");
    posButton9.setFocusable(false);
    transparentPanel9.add(posButton9);

    posButton8.setAction(calAction);
    posButton8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/2_32.png")));
    posButton8.setActionCommand("2");
    posButton8.setFocusable(false);
    transparentPanel9.add(posButton8);

    posButton7.setAction(calAction);
    posButton7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/3_32.png")));
    posButton7.setActionCommand("3");
    posButton7.setFocusable(false);
    transparentPanel9.add(posButton7);

    posButton10.setAction(calAction);
    posButton10.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/dot_32.png")));
    posButton10.setActionCommand(".");
    posButton10.setFocusable(false);
    transparentPanel9.add(posButton10);

    posButton11.setAction(calAction);
    posButton11.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/0_32.png")));
    posButton11.setActionCommand("0");
    posButton11.setFocusable(false);
    transparentPanel9.add(posButton11);

    posButton12.setAction(calAction);
    posButton12.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/clear_32.png")));
    posButton12.setActionCommand(com.floreantpos.POSConstants.CLEAR);
    posButton12.setFocusable(false);
    transparentPanel9.add(posButton12);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.weighty = 1.0;
    gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 0);
    transparentPanel8.add(transparentPanel9, gridBagConstraints);

    transparentPanel10.setLayout(new java.awt.GridLayout(0, 1, 5, 5));

    buttonGroup1.add(btnMasterCard);
    btnMasterCard.setAction(cardAction);
    btnMasterCard.setText("<html><body><p align='center'>MasterCard</p></body></html>");
    btnMasterCard.setActionCommand("MASTER_CARD");
    btnMasterCard.setPreferredSize(new java.awt.Dimension(90, 0));
    transparentPanel10.add(btnMasterCard);

    buttonGroup1.add(btnVisa);
    btnVisa.setAction(cardAction);
    btnVisa.setText("<html><body><p align='center'>Visa</p></body></html>");
    btnVisa.setActionCommand("VISA_CARD");
    btnVisa.setPreferredSize(new java.awt.Dimension(90, 0));
    transparentPanel10.add(btnVisa);

    buttonGroup1.add(btnVR);
    btnVR.setAction(cardAction);
    btnVR.setText("<html><body><p align='center'>Vale Refeição</p></body></html>");
    btnVR.setActionCommand("VR_CARD");
    btnVR.setPreferredSize(new java.awt.Dimension(90, 0));
    transparentPanel10.add(btnVR);

    buttonGroup1.add(btnTR);
    btnTR.setAction(cardAction);
    btnTR.setText("<html><body><p align='center'>Ticket Restaurante</p></body></html>");
    btnTR.setActionCommand("TR_CARD");
    btnTR.setPreferredSize(new java.awt.Dimension(90, 0));
    transparentPanel10.add(btnTR);

    buttonGroup1.add(btnOther);
    btnOther.setAction(cardAction);
    btnOther.setText("<html><body><p align='center'>Outro</p></body></html>");
    btnOther.setSelected(true);
    btnOther.setActionCommand("OTHER_CARD");
    btnOther.setPreferredSize(new java.awt.Dimension(90, 0));
    transparentPanel10.add(btnOther);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.weighty = 1.0;
    gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
    transparentPanel8.add(transparentPanel10, gridBagConstraints);

    transparentPanel7.setLayout(new java.awt.GridLayout(0, 1, 5, 5));

    transparentPanel1.setLayout(new java.awt.BorderLayout(5, 5));

    transparentPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
    jLabel1.setText(com.floreantpos.POSConstants.TIP_VALUE);
    transparentPanel1.add(jLabel1, java.awt.BorderLayout.NORTH);

    tfGratuityAmount.setFont(new java.awt.Font("Tahoma", 1, 12));
    tfGratuityAmount.setText("0");
    tfGratuityAmount.setEditable(false);
    tfGratuityAmount.setBackground(Color.WHITE);
    transparentPanel1.add(tfGratuityAmount, java.awt.BorderLayout.CENTER);

    lblCSymbol1.setText(Application.getCurrencySymbol());
    transparentPanel1.add(lblCSymbol1, java.awt.BorderLayout.WEST);

    transparentPanel7.add(transparentPanel1);

    transparentPanel4.setLayout(new java.awt.BorderLayout(5, 5));

    transparentPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
    jLabel3.setText(com.floreantpos.POSConstants.AUT_CODE);
    jLabel3.setEnabled(true);
    transparentPanel4.add(jLabel3, java.awt.BorderLayout.NORTH);

    tfAuthorizationCode.setFont(new java.awt.Font("Tahoma", 1, 12));
    tfAuthorizationCode.setText("0");
    tfAuthorizationCode.setEnabled(true);
    tfAuthorizationCode.setEditable(false);
    tfAuthorizationCode.setBackground(Color.WHITE);
    transparentPanel4.add(tfAuthorizationCode, java.awt.BorderLayout.CENTER);

    jLabel4.setText("CA");
    jLabel4.setEnabled(true);
    transparentPanel4.add(jLabel4, java.awt.BorderLayout.WEST);

    transparentPanel7.add(transparentPanel4);

    transparentPanel2.setLayout(new java.awt.BorderLayout(5, 5));

    transparentPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
    jLabel2.setText(com.floreantpos.POSConstants.LAUNCHED_CARD);
    transparentPanel2.add(jLabel2, java.awt.BorderLayout.NORTH);

    tfCardAmount.setFont(new java.awt.Font("Tahoma", 1, 12));
    tfCardAmount.setText("0");
    tfCardAmount.setEditable(false);
    tfCardAmount.setBackground(Color.WHITE);
    transparentPanel2.add(tfCardAmount, java.awt.BorderLayout.CENTER);

    lblCSymbol2.setText(Application.getCurrencySymbol());
    transparentPanel2.add(lblCSymbol2, java.awt.BorderLayout.WEST);

    transparentPanel7.add(transparentPanel2);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
    transparentPanel8.add(transparentPanel7, gridBagConstraints);

    add(transparentPanel8, java.awt.BorderLayout.CENTER);

  }

  private void btnSettleActionPerformed(java.awt.event.ActionEvent evt) {
    try {
      try {
        String gratuity = tfGratuityAmount.getText();
        if (gratuity == null || gratuity.trim().equals("")) {
          gratuityAmount = 0;
        } else {
          gratuityAmount = numberFormat.parse(gratuity).doubleValue();
        }
      } catch (NumberFormatException x) {
        POSMessageDialog.showError(com.floreantpos.POSConstants.INCORRECT_TIP_VALUE);
        return;
      }
      double tenderedAmount = 0;
      try {
        tenderedAmount = numberFormat.parse(tfCardAmount.getText()).doubleValue();
      } catch (NumberFormatException x) {
        POSMessageDialog.showError(com.floreantpos.POSConstants.INVALID_AMOUNT, x);
        return;
      }

      if (tenderedAmount == 0 || tenderedAmount - gratuityAmount < 0) {
        POSMessageDialog.showError(com.floreantpos.POSConstants.INSUFFICIENT_VALUE);
        return;
      }
      
    if(!isTicketDiscountOk()) {
      POSMessageDialog.showError("Valor do Desconto deve ser menor que o Valor do Pedido");
      return;
    }      

      String authorizationCode = tfAuthorizationCode.getText();

      if (authorizationCode.isEmpty()) {
        authorizationCode = "0";
      }

      if (getCardType() == CARD_TYPE_CREDIT) {
        settleTickets(tenderedAmount, gratuityAmount, new CreditCardTransaction(), getWhichCard(), authorizationCode + "2", taxId);
      }
      if (getCardType() == CARD_TYPE_DEBIT) {
        settleTickets(tenderedAmount, gratuityAmount, new DebitCardTransaction(), getWhichCard(), authorizationCode + "1", taxId);
      }
      if (getCardType() == CARD_TYPE_BENEFIT) {
        settleTickets(tenderedAmount, gratuityAmount, new CreditCardTransaction(), getWhichCard(), authorizationCode + "3", taxId);
      } 
    } catch (Exception e) {
      POSMessageDialog.showError(com.floreantpos.POSConstants.UNEXPECTED_ERROR, e);
    }
  }

  private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {
    RootView.getInstance().showView(SwitchboardView.VIEW_NAME);
  }

  private void btnChangePMethodActionPerformed(java.awt.event.ActionEvent evt) {
    if (giftCertStatus) {
      changePaymentMethod(taxId);
    } else {
      changePaymentMethod(giftCertStatus, guestID, taxId);
    }
  }

  private JTextField getFocusedTextField() {
    if (!tfCardAmount.isFocusable()) {
      return tfAuthorizationCode;
    }
    if (tfGratuityAmount.hasFocus()) {
      return tfGratuityAmount;
    }
    if (tfAuthorizationCode.hasFocus()) {
      return tfAuthorizationCode;
    }
    if (tfCardAmount.hasFocus()) {
      return tfCardAmount;
    }
    tfGratuityAmount.requestFocus();
    return tfGratuityAmount;
  }

  private Action cardAction = new AbstractAction() {
    public void actionPerformed(ActionEvent e) {
      whichCard = e.getActionCommand();
    }
  };

  private Action calAction = new AbstractAction() {
    public void actionPerformed(ActionEvent e) {
      JTextField tf = getFocusedTextField();

      String s = e.getActionCommand();
      if (s.equals(com.floreantpos.POSConstants.CLEAR)) {
        tf.setText("0");
      } else if (s.equals(".")) {
        if (tf == tfAuthorizationCode) {
          return;
        }
        if (tf.getText().indexOf(',') < 0) {
          tf.setText(tf.getText() + ",");
        }
      } else {
        String string = tf.getText();
        int index = string.indexOf(',');
        if (index < 0) {
          double value = 0;
          try {
            value = numberFormat.parse(string).doubleValue();
          } catch (Exception x) {
            Toolkit.getDefaultToolkit().beep();
          }
          if (value == 0) {
            tf.setText(s);
          } else {
            if (tf == tfAuthorizationCode) {
              tf.setText(string + s);
            } else {
              tf.setText(Application.formatTextAsNumber(string + s));
            }
          }
        } else {
          if (tf == tfAuthorizationCode) {
            tf.setText(string + s);
          } else {
            tf.setText(Application.formatTextAsNumber(string + s));
          }
        }
      }
      if (tf == tfGratuityAmount) {
        double gAmount = 0;
        double amount = getDueAmount();
        try {
          gAmount = numberFormat.parse(tfGratuityAmount.getText()).doubleValue();
        } catch (Exception x) {
          return;
        }
        tfCardAmount.setText(Application.formatNumber(amount + gAmount));
      }
    }
  };

  private com.floreantpos.swing.PosButton btnCancel;
  private com.floreantpos.swing.PosButton btnChangePMethod;
  private com.floreantpos.swing.POSToggleButton btnMasterCard;
  private com.floreantpos.swing.POSToggleButton btnOther;
  private com.floreantpos.swing.POSToggleButton btnVR;
  private com.floreantpos.swing.POSToggleButton btnTR;
  private com.floreantpos.swing.PosButton btnSettle;
  private com.floreantpos.swing.POSToggleButton btnVisa;
  private javax.swing.ButtonGroup buttonGroup1;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JLabel jLabel4;
  private javax.swing.JLabel lblCSymbol1;
  private javax.swing.JLabel lblCSymbol2;
  private com.floreantpos.swing.PosButton posButton1;
  private com.floreantpos.swing.PosButton posButton10;
  private com.floreantpos.swing.PosButton posButton11;
  private com.floreantpos.swing.PosButton posButton12;
  private com.floreantpos.swing.PosButton posButton2;
  private com.floreantpos.swing.PosButton posButton3;
  private com.floreantpos.swing.PosButton posButton4;
  private com.floreantpos.swing.PosButton posButton5;
  private com.floreantpos.swing.PosButton posButton6;
  private com.floreantpos.swing.PosButton posButton7;
  private com.floreantpos.swing.PosButton posButton8;
  private com.floreantpos.swing.PosButton posButton9;
  private javax.swing.JTextField tfAuthorizationCode;
  private javax.swing.JTextField tfCardAmount;
  private javax.swing.JTextField tfGratuityAmount;
  private com.floreantpos.swing.TransparentPanel transparentPanel1;
  private com.floreantpos.swing.TransparentPanel transparentPanel10;
  private com.floreantpos.swing.TransparentPanel transparentPanel2;
  private com.floreantpos.swing.TransparentPanel transparentPanel3;
  private com.floreantpos.swing.TransparentPanel transparentPanel4;
  private com.floreantpos.swing.TransparentPanel transparentPanel5;
  private com.floreantpos.swing.TransparentPanel transparentPanel7;
  private com.floreantpos.swing.TransparentPanel transparentPanel8;
  private com.floreantpos.swing.TransparentPanel transparentPanel9;

  public int getCardType() {
    return cardType;
  }

  public void setCardType(int cardType) {
    this.cardType = cardType;

    if (cardType == CARD_TYPE_DEBIT) {
      btnVisa.setEnabled(true);
      btnMasterCard.setEnabled(true);
      btnOther.setEnabled(true);
      btnVR.setEnabled(false);
      btnTR.setEnabled(false);
      TitledBorder titledBorder = new TitledBorder(com.floreantpos.POSConstants.DEBIT_CARD_TRANSACTION);
      titledBorder.setTitleJustification(TitledBorder.CENTER);
      setBorder(titledBorder);
    } else if (cardType == CARD_TYPE_CREDIT) {
      btnVisa.setEnabled(true);
      btnMasterCard.setEnabled(true);
      btnOther.setEnabled(true);
      btnVR.setEnabled(false);
      btnTR.setEnabled(false);
      TitledBorder titledBorder = new TitledBorder(com.floreantpos.POSConstants.CREDIT_CARD_TRANSACTION);
      titledBorder.setTitleJustification(TitledBorder.CENTER);
      setBorder(titledBorder);
    } else {
      btnVisa.setEnabled(true);
      btnMasterCard.setEnabled(false);
      btnVR.setEnabled(true);
      btnTR.setEnabled(true);
      btnOther.setEnabled(true);
      TitledBorder titledBorder = new TitledBorder(com.floreantpos.POSConstants.CARD_BENEFITS);
      titledBorder.setTitleJustification(TitledBorder.CENTER);
      setBorder(titledBorder);
    }
  }

  public String getWhichCard() {
    return whichCard;
  }

  public void setWhichCard(String whichCard) {
    this.whichCard = whichCard;
  }

  @Override
  public void updateView() {
    tfCardAmount.setText(Application.formatNumber(getDueAmount() + getTotalGratuity()));
    tfGratuityAmount.setText(Application.formatNumber(getTotalGratuity()));
  }
}
